package com.yyw.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyw.common.utils.PageUtils;
import com.yyw.common.utils.Query;
import com.yyw.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.yyw.gulimall.product.dao.AttrGroupDao;
import com.yyw.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.yyw.gulimall.product.entity.AttrEntity;
import com.yyw.gulimall.product.entity.AttrGroupEntity;
import com.yyw.gulimall.product.service.AttrAttrgroupRelationService;
import com.yyw.gulimall.product.service.AttrGroupService;
import com.yyw.gulimall.product.service.AttrService;
import com.yyw.gulimall.product.vo.AttrAttrGroupVo;
import com.yyw.gulimall.product.vo.AttrGroupRelationVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
@RequiredArgsConstructor
@Slf4j
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    /**
     * 属性与属性分组关联接口
     */
    private final AttrAttrgroupRelationService attrAttrgroupRelationService;
    private final AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    /**
     * 属性接口
     */
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (catelogId != 0) {
            queryWrapper.lambda().eq(AttrGroupEntity::getCatelogId, catelogId);

        }
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.lambda().and(e -> {
                e.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getAttrGroupName, key);
            });
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getAttrGroupAllAttr(long attrGroupId) {
        // 获取所有关联的数据
        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId);
        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationService.list(queryWrapper);
        // 取出属性ID
        List<Long> attrIds = list.stream().map(e -> e.getAttrId()).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(attrIds)) {
            return attrService.listByIds(attrIds);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delAttrRelationGroup(List<AttrGroupRelationVo> relation) {
        // 查询关联表的数据
        List<Long> ids = attrAttrgroupRelationDao.getIdsByAttrIdAndAttrGroupId(relation);
        if (!CollectionUtils.isEmpty(ids)) {
            attrAttrgroupRelationService.removeByIds(ids);
        }
    }

    @Override
    public List<AttrAttrGroupVo> getAttrByAttrGroupByCatelogId(long catelogId) {
        // 获取分组
        List<AttrGroupEntity> groups = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>()
                .lambda().eq(AttrGroupEntity::getCatelogId, catelogId));
        if (!CollectionUtils.isEmpty(groups)) {
            List<AttrAttrGroupVo> collect = groups.stream().map(e -> {
                List<AttrEntity> attr = getAttrGroupAllAttr(e.getAttrGroupId());
                AttrAttrGroupVo vo = new AttrAttrGroupVo();
                BeanUtils.copyProperties(e, vo);
                vo.setAttrs(attr);
                return vo;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }


}